package com.http.controller;

import com.http.config.Result;
import com.http.intg.HttpServiceClient;
import com.http.pojo.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * @ClassName: RestHttpController
 * @Description: RestHttp操作接口
 * @author: QT
 * @date: 2021年04月19日 上午10:25:31
 */
@Api(tags = "RestHttpController", description = "RestHttp操作接口")
@RestController
@RequestMapping("/resthttp")
public class RestHttpController {
    public static final Logger log = LoggerFactory.getLogger(RestHttpController.class);

    @Autowired
    HttpServiceClient httpServiceClient;

    @ApiOperation("按员工ID获取员工列表")
    @GetMapping("/queryUserById")
    public @ResponseBody
    Result queryUserById(@ApiParam(name = "id", value = "用户ID", required = true)
                         @RequestParam int id) throws IOException {
        try{
            String username = httpServiceClient.getUserById(id);
            log.info("按员工ID获取员工："+username);
            return Result.succResult(username);
        }catch(IOException e){
            log.info("Exception: "+e);
            return Result.failResult("Exception: "+e);
        }
    }

    /**
     * @param pageNow 当前页
     * @param pageSize 每页大小
     * @return
     */
    @ApiOperation("获取员工列表")
    @GetMapping("/queryUserList")
    public @ResponseBody
    Result queryUserList(@ApiParam(name = "pageNow", value = "当前页", required = true)
                         @RequestParam int pageNow,
                         @ApiParam(name = "pageSize", value = "每页大小", required = true)
                         @RequestParam int pageSize) throws IOException {
        try{
            String userList = httpServiceClient.getUserList(pageNow,pageSize);
            log.info("获取员工列表："+userList);
            return Result.succResult(userList);
        }catch (IOException e){
            log.info("Exception: "+e);
            return Result.failResult("Exception: "+e);
        }
    }

    @ApiOperation("员工新增")
    @PostMapping("/createUser")
    public @ResponseBody
    Result createUser(@ApiParam(value = "用户信息", required = true) @RequestBody User user) throws IOException {
        try{
            String result = httpServiceClient.createUser(user);
            log.info("员工新增："+result);
            return Result.succResult(result);
        }catch (IOException e){
            log.info("Exception: "+e);
            return Result.failResult("Exception: "+e);
        }
    }

    @ApiOperation("员工修改")
    @PostMapping("/updateUser/{id}")
    public @ResponseBody
    Result updateUserResult(
            @ApiParam(value = "用户ID", required = true) @PathVariable Integer id,
            @ApiParam(value = "Token", required = true) @RequestHeader String token,
            @ApiParam(value = "用户信息", required = true)  @RequestBody User user) throws IOException {
        try{
            String result = httpServiceClient.updateUser(id,token,user);
            log.info("员工修改："+result);
            return Result.succResult(result);
        }catch (IOException e){
            log.info("Exception: "+e);
            return Result.failResult("Exception: "+e);
        }
    }

    @ApiOperation("员工删除")
    @DeleteMapping("/deleteUser/{id}")
    public @ResponseBody
    Result deleteUserResult(@ApiParam(value = "用户ID", required = true) @PathVariable Integer id) throws IOException {
        try {
            String result = httpServiceClient.deleteUser(id);
            log.info("员工删除："+result);
            return Result.succResult(result);
        } catch (IOException e) {
            log.info("Exception: "+e);
            return Result.failResult("Exception: "+e);
        }
    }
}
